<?php

include_once './lib/fun.php';
// 执行注册登录页
include_once './upload.class.php';
class Register
{
    protected $username;
    protected $number;
    protected $email;
    protected $money;
    protected $face;
    protected $info;
    public $return = array();
    public function __construct($username, $number, $email, $money, $face, $info)
    {
        $checkRes = $this->checkReg($username, $number, $email, $money, $face, $info);
        if (!$checkRes) {
            returnToPage('register.php', $this->return);
        }
    }
    public function checkReg($username, $number, $email, $money, $face, $info)
    {
        if (!$username) {
            $this->writeReturn(0, '账号不能为空');
            return false;
        }
        if (!$number) {
            $this->writeReturn(0, '学号不能为空');
            return false;
        }
        if (!$email) {
            $this->writeReturn(0, '邮箱不能为空');
            return false;
        }
        if (!$face) {
            $this->writeReturn(0, '头像不能为空');
            return false;
        }
        if (strlen($info)>50) {
            $this->writeReturn(0, '个人简介超出范围');
            return false;
        }
        if (!is_numeric($number) || strlen($number)>8 || $number < 0) {
            $this->writeReturn(0, '学号不合法');
            return false;
        }
        $this->username = $username;
        $this->number = $number;
        $this->email = $email;
        $this->face = $face;
        $this->money = is_numeric($money)?$money:0;
        $this->info = $info?$info:'';
        return true;
    }
    public function sqlInsert()
    {
        $con = new PDO('mysql:host=localhost;dbname=smsystem', 'root', 'root');
        $con->query('set names utf8');
        $sql = "select count(id) as count from user where number='{$this->number}' limit 1";
        $stat = $con->query($sql);
        $res = $stat->fetch(PDO::FETCH_ASSOC);
        if ($res['count'] == 0) {
            $fileObject = new UploadFile($this->face);
            $res = $fileObject->checkUpload();
            if ($res['result']) {
                $this->face = $res['uploadPath'];
            } else {
                $this->writeReturn(0, $res['message']);
                returnToPage('register.php', $this->return);
                return false;
            }
            unset($res);
            unset($stat);
            unset($sql);
            $sql = "insert into user(name,number,email,money,info,face) value('{$this->username}','{$this->number}','{$this->email}','{$this->money}','{$this->info}','{$this->face}')";
            $rescount = $con->exec($sql);
            if ($rescount > 0) {
                return $con->lastInsertId();
            } else {
                $this->writeReturn(0, '插入失败');
                returnToPage('register.php', $this->return);
                return false;
            }
        } else {
            $this->writeReturn(0, '该学号已经注册');
            return false;
        }
    }
    private function writeReturn($result, $msg)
    {
        $this->return['result'] = $result;
        $this->return['msg'] = $msg;
        returnToPage('register.php', $this->return);
        return false;
    }
}
